package com.yangjifang.mr.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * KEYIN map阶段输出的key
 * VALUEIN map阶段暑促的value
 *
 */
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    IntWritable v =new IntWritable();

    /**
     * 同样的key一次性处理，这样才能体现出reduce这个过程的作用 不然怎么归总呢
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;

        for(IntWritable value : values){
            sum += value.get();
        }

        v.set(sum);

        context.write(key,v);

    }

}
